package com.itheima.action;

import com.itheima.domain.Customer;
import com.itheima.domain.Linkman;
import com.itheima.service.CustomerService;
import com.itheima.service.LinkmanService;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import org.apache.commons.fileupload.FileUpload;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.util.List;

public class LinkmanAction extends ActionSupport implements ModelDriven<Linkman>{

	private Linkman linkman = new Linkman();
	@Override
	public Linkman getModel() {
		return linkman;
	}
	public Linkman getLinkman() {
		return linkman;
	}

	private LinkmanService service;
	public void setService(LinkmanService service) {
		this.service = service;
	}

	private CustomerService customerService;
	public void setCustomerService(CustomerService customerService) {
		this.customerService = customerService;
	}

	private List list;
	public List getList() {
		return list;
	}

	private File uplod;	//上传文件
	private String uplodFileName;	//上传文件名

	//生成get和set方法
	public File getUplod() {
		return uplod;
	}
	public void setUplod(File uplod) {
		this.uplod = uplod;
	}
	public String getUplodFileName() {
		return uplodFileName;
	}
	public void setUplodFileName(String uploadFileName) {
		this.uplodFileName = uploadFileName;
	}

	//跳转添加页面
	public String add() {
		list = customerService.list();
		return "add";
	}

	//保存添加
	public String saveAdd() throws IOException {
		//上传文件
		if (uplod != null){
			File file = new File("D:\\upload/" + uplodFileName);
			FileUtils.copyFile(uplod,file);
		}

		service.saveAdd(linkman);
		return "toList";
	}

	//显示联系人列表
	public String list(){
		list = service.list();
		return "list";
	}

	//模糊查询
	public String likeSerch(){
		list = service.likeSerch(linkman);
		return "list";
	}

	//跳转修改页面
	public String edit(){
		linkman = service.edit(linkman);
        list = customerService.list();
        return "edit";
	}

	//保存联系人修改
    public String saveEdit(){
	    service.saveEdit(linkman);
	    return "toList";
    }

	//删除联系人
	public String delete(){
		service.delete(linkman);
		return "toList";
	}
}
